package io.wins.vertx.service.impl;

import io.vertx.core.Vertx;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.wins.vertx.entity.Classification;
import io.wins.vertx.service.BaseService;
import io.wins.vertx.service.ClassService;

public class ClassServiceImpl extends BaseService implements ClassService {
    public ClassServiceImpl(final Vertx vertx) {
        this.vertx = vertx;
    }

    @Override
    public ClassService getCustClasses(final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_class where type = '0' and active = '1' order by seq asc");
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public ClassService getAllClasses(final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_class where active = '1' order by seq asc");
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public ClassService disClasses(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_class set active = '0' where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.deleteSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public ClassService loadClass(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_class where id = ? and active = '0'")
            .put(PARAMS, new JsonArray().add(id));
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public ClassService addClasses(final Classification c, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "insert into ds_class (id, name, code, seq) VALUES (?, ?, ?, ?)")
            .put(PARAMS, new JsonArray()
                .add(c.getId())
                .add(c.getName())
                .add(c.getCode())
                .add(c.getSeq()));
        this.insertSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public ClassService saveClass(final Classification c, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_class set name = ?, code = ?, seq = ? where id = ?")
            .put(PARAMS, new JsonArray()
                .add(c.getName())
                .add(c.getCode())
                .add(c.getSeq())
                .add(c.getId()));
        this.updateSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }
}
